package com.guigu.www.erp.dehao.controller;

import com.guigu.www.erp.common.pojo.PageVo;
import com.guigu.www.erp.dehao.pojo.Role;
import com.guigu.www.erp.dehao.pojo.User;
import com.guigu.www.erp.dehao.service.RoleService;
import com.guigu.www.erp.dehao.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@RestController
@RequestMapping("/Role")
public class RoleController {

    @Autowired
    private RoleService roleService;
    @Autowired
    private UserService userService;

    @DeleteMapping("{roleId}")
    public void deleteByPrimaryKey(@PathVariable Integer roleId) {
        roleService.deleteByPrimaryKey(roleId);
    }

    @PostMapping("/add")
    public void insertSelective(Role role) {
        roleService.insertSelective(role);
    }

    @GetMapping("/byRoleId")
    public Role selectByPrimaryKey(HttpServletRequest request) {
        User loginUser = (User) request.getSession().getAttribute("loginUser");
        User user = userService.selectByPrimaryKey(loginUser.getUserId());
        Role role = roleService.selectByPrimaryKey(loginUser.getRole().getRoleId());
        if (role!=null){
            role.setUser(user);
        }
        return role;
    }

    @PutMapping("/up")
    public void updateByPrimaryKeySelective(Role role) {
        roleService.updateByPrimaryKeySelective(role);
    }

    @GetMapping("/findAll")
    public PageVo<Role> findAllRole(Integer pageNum,Integer pageSize) {
        return roleService.findAllRole(pageNum, pageSize);
    }
    @GetMapping("/All")
    public List<Role> findRole() {
        return roleService.findRole();
    }

}
